<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/5
 * Time: 19:47
 */

namespace app\logic\index\production;




use app\logic\BaseLogic;

class ProductLogic extends BaseLogic
{
    protected $productModel;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->productModel = \app\model\production\ProductionModel::getInstance();
    }

    public static function getInstance()
    {
        if(is_object(self::$instance)){
            return self::$instance;
        }

        self::$instance = new self();

        return self::$instance;
    }

    public function getList($param)
    {
        $where = [
            'isdel'=>0,
            'status'=>1
        ];

        $order = ['sort','asc'];

        $field = ['production_sn','title','subtitle','sale_price','origin_price','main_image'];

        $data = $this->productModel->index($where, $field, $order);

        foreach($data['data'] as $val){
            $val['image'] = $val['main_image']??'';
            $val['title'] = $val['title']??'';
            $val['describe'] = $val['subtitle']??'';
            $val['count'] = $val['sale_price']??0;
            $val['delCount'] = $val['origin_price']??0;
        }
        return $data;
    }

    public function getDetail($param)
    {
        $where = [
            'status'=>1,
            'isdel'=>0,
            'production_sn'=>$param['production_sn'],
        ];
        $detail = $this->productModel->getDetail($where);

        $detail->images = $detail['images'] ? json_decode($detail['images'],true) : [];
        $detail->desc_content = $detail['desc_content'] ? json_decode($detail['desc_content'],true) : [];

        $productionAttrs = ProductionAttrModel::getInstance()->getWhere(['productionid'=>$detail['productionid']])->get();
        $attrs = [];
        foreach ($productionAttrs as $val){
            $attrs[] = [
                'sale_attr_id'=>$val['sale_attr_id'],
                'name'=>$val['name'],
                'unit'=>$val['unit'],
                'sale_price'=>$val['sale_price'],
                'origin_price'=>$val['origin_price'],
            ];
        }
        $detail->attrs = $attrs;
        $detail->sale_count = mt_rand(100,1000);

        $detail = $this->unsetCommonFields($detail);

        return $detail;
    }
}